package com.examsage.examsage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.examsage.examsage.model.TeachingPoint;
import com.examsage.examsage.mapper.TeachingPointMapper;
import com.examsage.examsage.model.vo.ResponseVO;
import com.examsage.examsage.service.ITeachingPointService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author examsage
 * @since 2025-07-02
 */
@Service
public class TeachingPointServiceImpl extends ServiceImpl<TeachingPointMapper, TeachingPoint> implements ITeachingPointService {

    @Resource
    private TeachingPointMapper teachingPointMapper;

    @Override
    public ResponseVO addTeachingPoint(TeachingPoint teachingPoint) {
        ResponseVO responseVO = new ResponseVO();
        if(teachingPoint.getStatus() == null) {
            teachingPoint.setStatus(true);
        }
        teachingPoint.setGmtCreate(LocalDateTime.now());
        teachingPoint.setGmtModified(LocalDateTime.now());
        if (teachingPointMapper.insert(teachingPoint) > 0){
            responseVO.setStatusCode(200);
            responseVO.setMessage("添加教学点成功");
            responseVO.setData(teachingPoint);
        } else {
            responseVO.setStatusCode(400);
            responseVO.setMessage("添加教学点失败");
            responseVO.setData(null);
        }
        return responseVO;
    }


    @Override
    public ResponseVO getTeachingPoint(int page, int size, String pointName, Boolean status) {
        // 默认值处理
        if (page <= 0) page = 1;
        if (size <= 0) size = 10;

        QueryWrapper<TeachingPoint> queryWrapper = new QueryWrapper<>();

        // 按教学点名称模糊查询
        if (!StringUtils.isBlank(pointName)) {
            queryWrapper.like("point_name", pointName);
        }

        // 按合作状态筛选
        if (status != null) {
            queryWrapper.eq("status", status ? 1 : 0);
        }

        // 分页查询
        Page<TeachingPoint> pageQuery = new Page<>(page, size);

        ResponseVO responseVO = new ResponseVO();

        try {
            Page<TeachingPoint> resultPage = this.page(pageQuery, queryWrapper);
            responseVO.setStatusCode(200);
            responseVO.setMessage("查询教学点成功");
            responseVO.setData(resultPage);
        } catch (Exception e) {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询教学点失败");
            responseVO.setData(null);
        }

        return responseVO;
    }


    @Override
    public ResponseVO updateTeachingPoint(Integer id, TeachingPoint teachingPoint) {
        // 构建更新条件
        UpdateWrapper<TeachingPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        // 动态更新非空字段
        if (teachingPoint.getPointCode() != null && !teachingPoint.getPointCode().trim().isEmpty()) {
            updateWrapper.set("point_code", teachingPoint.getPointCode());
        }
        if (teachingPoint.getPointName() != null && !teachingPoint.getPointName().trim().isEmpty()) {
            updateWrapper.set("point_name", teachingPoint.getPointName());
        }
        if (teachingPoint.getAddress() != null && !teachingPoint.getAddress() .trim().isEmpty()) {
            updateWrapper.set("address", teachingPoint.getAddress());
        }
        if (teachingPoint.getManagerName() != null && !teachingPoint.getManagerName().trim().isEmpty()) {
            updateWrapper.set("manager_name", teachingPoint.getManagerName());
        }
        if (teachingPoint.getManagerPhone() != null && !teachingPoint.getManagerPhone().trim().isEmpty()) {
            updateWrapper.set("manager_phone", teachingPoint.getManagerPhone());
        }
        if (teachingPoint.getStatus() != null) {
            updateWrapper.set("status", teachingPoint.getStatus());
        }

        ResponseVO responseVO = new ResponseVO();
        if (teachingPointMapper.update(null, updateWrapper) > 0) {
            responseVO.setStatusCode(200);
            responseVO.setMessage("修改教学点成功");
            responseVO.setData(teachingPointMapper.selectById(id));
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("修改教学点失败");
        }
        return responseVO;
    }




    @Override
    public ResponseVO deleteTeachingPoint(Integer id) {
        ResponseVO responseVO = new ResponseVO();
        if (teachingPointMapper.deleteById(id) > 0){
            responseVO.setStatusCode(200);
            responseVO.setMessage("删除教学点成功");
            responseVO.setData(null);
        } else {
            responseVO.setStatusCode(400);
            responseVO.setMessage("删除教学点失败");
            responseVO.setData(null);
        }
        return responseVO;
    }


}
